package util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Writer;
import java.util.ArrayList;

import net.zemberek.erisim.Zemberek;
import net.zemberek.islemler.KokBulucu;
import net.zemberek.tr.yapi.TurkiyeTurkcesi;
import net.zemberek.yapi.Kok;
import zemberek.core.io.Strings;

public final class Utility {
/* 
 * Bu bölümde Hazırlık aşamasındaki işler yapılacaktır.
 */
	public static void hazirlik() {
		try {
			// Read StopWords from File
			FileReader fReader = new FileReader(
					"C://Workspace2//data//StopWordList.txt");
			BufferedReader bReader = new BufferedReader(fReader);
			String line;

			ArrayList<String> stopWords = new ArrayList<String>();
			while ((line = bReader.readLine()) != null) {
				stopWords.add(line);
			}
			fReader.close();
			bReader.close();
			/*
			 * Corpus dan okunma işlemi
			 */
			FileReader fReader2 = new FileReader(
					"C://Workspace2//data//TextData.txt");
			BufferedReader bReader2 = new BufferedReader(fReader2);

			// Write file
			FileWriter fWriter = new FileWriter(
					"C://Workspace2//data//outTextData.txt");
			BufferedWriter bWriter = new BufferedWriter(fWriter);

			// Cümleler arası ayıraçlar
			String delims = "[ .,?!]+";
			String[] tokens = null;

			// Read the WORD from the file Line By Line
			// and checked for delimiters and StopWords
			String strLine;
			while ((strLine = bReader2.readLine()) != null) {
				// storeWordList.add(strLine);
				tokens = strLine.split(delims);

				for (String singleRawWord : tokens) {
					// Write word by word to output file

					// Change word to lowercase
					String aStrLowerCased = singleRawWord.toLowerCase();

					// Tirnak isaretleri kaldir ve eklerini at
					String strRemovedPunct = Strings.subStringUntilFirst(
							aStrLowerCased, "\'");

					// Baglaclari kaldir. Stopword listesine göre
					// Hazirlik 3.a. Ve-veya-de-da-dahi vb.
					if (!stopWords.contains(strRemovedPunct)) {
						bWriter.write(strRemovedPunct);
						((BufferedWriter) bWriter).newLine();
						// System.out.println(t);
					}
				}
			}
			// Close the input stream

			fReader2.close();
			fWriter.close();

			System.out.println("Your file has been written");

		} catch (Exception e) {// Catch exception if any
			System.err.println("Error: " + e.getMessage());
		}
	}

	public Utility() {

	}

	public static boolean kokSozluguOlustur() throws IOException {

		Zemberek zemberek = new Zemberek(new TurkiyeTurkcesi());
		KokBulucu kb = zemberek.kokBulucu();

		BufferedReader reader = new BufferedReader(new FileReader(
				"C://workspace2//data//sozcukler.txt"));
		BufferedWriter writer = new BufferedWriter(new FileWriter(
				"C://workspace2//data//KokSozlugu.txt", true));

		String line = null;
		// StringBuilder stringBuilder = new StringBuilder();
		// String ls = System.getProperty("line.separator");

		while ((line = reader.readLine()) != null) {

			Kok[] kokler = kb.kokBul(line);
			if (kokler.length > 0) {
				writer.write(kokler[0].icerik());
				writer.newLine();
			}

			// // stringBuilder.append(line);
			// if (i)
			// stringBuilder.append(".");
		}
		reader.close();
		writer.close();
		return true;
	}

	public static String readFileToString(String file, boolean i)
			throws IOException {
		BufferedReader reader = new BufferedReader(new FileReader(file));
		String line = null;
		StringBuilder stringBuilder = new StringBuilder();
		String ls = System.getProperty("line.separator");
		while ((line = reader.readLine()) != null) {
			stringBuilder.append(line);
			if (i)
				stringBuilder.append(".");
		}
		reader.close();
		return stringBuilder.toString();
	}

}
